package com.googlecode.yoohoo.xep.data.protocol;

import java.util.ArrayList;
import java.util.List;

import com.googlecode.yoohoo.xmppcore.protocol.parsing.ProtocolKey;
import com.googlecode.yoohoo.xmppcore.protocol.translation.ITranslator;
import com.googlecode.yoohoo.xmppcore.protocol.translation.IXmlWriter;
import com.googlecode.yoohoo.xmppcore.protocol.translation.XmlWriter;

public class Data implements ITranslator {
	public static final ProtocolKey PROTOCOL_KEY = new ProtocolKey("x", "jabber:x:data");
	
	private List<String> instructions;
	private String title;
	private List<Field> fields;
	private DataType type;
	private Reported reported;
	private List<Item> items;
	
	public List<String> getInstructions() {
		if (instructions == null)
			instructions = new ArrayList<String>();
		
		return instructions;
	}
	
	public String getTitle() {
		return title;
	}
	
	public List<Field> getFields() {
		if (fields == null)
			fields = new ArrayList<Field>();
		
		return fields;
	}
	
	public DataType getType() {
		return type;
	}
	
	public Reported getReported() {
		return reported;
	}
	
	public List<Item> getItems() {
		if (items == null)
			items = new ArrayList<Item>();
		
		return items;
	}
	
	public void setInstructions(List<String> instructions) {
		this.instructions = instructions;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	public void setFields(List<Field> fields) {
		this.fields = fields;
	}
	
	public void setType(DataType type) {
		this.type = type;
	}
	
	public void setReported(Reported report) {
		this.reported = report;
	}
	
	public void setItems(List<Item> items) {
		this.items = items;
	}

	@Override
	public String toXml() {
		IXmlWriter writer = new XmlWriter();
		writer.writeStartElement("x");
		writer.writeNamespace("jabber:x:data");
		if (type != null) {
			writer.writeAttribute("type", type.toString());
		}
		
		if (title != null) {
			writer.writeStartElement("title");
			writer.writeCharacters(title);
			writer.writeEndElement();
		}
		
		if (instructions != null) {
			for (String instruction : instructions) {
				writer.writeStartElement("instructions");
				writer.writeCharacters(instruction);
				writer.writeEndElement();
			}
		}
		
		if (fields != null) {
			for (Field field : fields) {
				writeField(writer, field);
			}
		}
		
		if (reported != null) {
			writer.writeStartElement("reported");
			if (reported.getFields() != null) {
				for (Field field : reported.getFields()) {
					writeField(writer, field);
				}
			}
			writer.writeEndElement();
		}
		
		if (items != null) {
			for (Item item : items) {
				writer.writeStartElement("item");
				if (item.getFields() != null) {
					for (Field field : item.getFields()) {
						writeField(writer, field);
					}
				}
				writer.writeEndElement();
			}
		}
		
		writer.writeEndElement();
		return writer.toXml();
	}

	private void writeField(IXmlWriter writer, Field field) {
		writer.writeStartElement("field");
		if (field.getType() != null) {
			writer.writeAttribute("type", field.getType().toString());
		}
		
		if (field.getLabel() != null) {
			writer.writeAttribute("label", field.getLabel());
		}
		
		if (field.getVar() != null) {
			writer.writeAttribute("var", field.getVar());
		}
		
		if (field.isRequired()) {
			writer.writeEmptyElement("required");
		}
		
		if (field.getDesc() != null) {
			writer.writeStartElement("desc");
			writer.writeCharacters(field.getDesc());
			writer.writeEndElement();
		}
		
		if (field.getOptions() != null) {
			for (Option option : field.getOptions()) {
				writer.writeStartElement("option");
				if (option.getLabel() != null) {
					writer.writeAttribute("label", option.getLabel());
				}
				
				writer.writeStartElement("value");
				writer.writeCharacters(option.getValue());
				writer.writeEndElement();
				
				writer.writeEndElement();
			}
		}
		
		if (field.getValues() != null) {
			for (String value : field.getValues()) {
				writer.writeStartElement("value");
				writer.writeCharacters(value);
				writer.writeEndElement();
			}
		}
		
		writer.writeEndElement();
	}
	
}
